define(['zepto'], function($) {
	var me;
	var Storage = {
		view: null
	}

	Storage.init = function(view) {
		this.view = view;
		me = Storage.view;
	}



	var Session = {
		saveObject: function(key, object) {
			var stackKey = key + '-stack';
			var dataStack = Piece.Session.loadObject(stackKey);

			var items = (dataStack) ? dataStack.items : []; //flightInfoData


			//创建航班记录堆栈
			if (items && items.length != 0) {
				dataStack.dataCurrentIndex = items.length - 1;
				items[dataStack.dataCurrentIndex] = object;

			} else {
				dataStack = dataStack ? dataStack : {};
				items = [];
				dataStack.dataCurrentIndex = 0;
				items.push(object);
			}

			dataStack.items = items;

			Piece.Session.saveObject(stackKey, dataStack);
			Piece.Session.saveObject(key, object);
		},
		loadObject: function(key) {
			var stackKey = key + '-stack';
			var currentObject;
			if (me.stepForward > 0) {
				currentObject = Piece.Session.loadObject(key);
			} else {
				if (Piece.Session.loadObject(stackKey)) {

					var dataStack = Piece.Session.loadObject(stackKey);
					console.log(dataStack);
					console.log('--stack--');
					console.log('dataCurrentIndex = ' + dataStack.dataCurrentIndex);
					console.log('data.length = ' + dataStack.items.length);
					currentObject = dataStack.items[dataStack.dataCurrentIndex];
				} else {

					console.log('--null--');
					currentObject = Piece.Session.loadObject(key);
					var dataStack = {};
					var items = [];
					dataStack.dataCurrentIndex = 0;
					items.push(currentObject);

					dataStack.items = items;

					Piece.Session.saveObject(stackKey, dataStack);
				}

			}
			
			return currentObject;
		},

		pushObject: function(key, object) {
			var stackKey = key + '-stack';
			var dataStack = Piece.Session.loadObject(stackKey);

			var items = (dataStack) ? dataStack.items : []; //flightInfoData


			//创建航班记录堆栈
			if (items && items.length != 0) {
				dataStack.dataCurrentIndex = items.length;
				items[dataStack.dataCurrentIndex] = object;

			} else {
				dataStack = dataStack ? dataStack : {};
				items = [];
				dataStack.dataCurrentIndex = 0;
				items.push(object);
			}

			dataStack.items = items;

			Piece.Session.saveObject(stackKey, dataStack);
		},

		pullObject: function(key) {
			var stackKey = key + '-stack';
			var dataStack = Piece.Session.loadObject(stackKey);

			var items = (dataStack) ? dataStack.items : [];
			dataStack = dataStack ? dataStack : {};
			var currentObject = null;
			console.log('stack.length = ' + items.length);
			if (items && items.length != 0) {

				currentObject = items[items.length - 1];
				if (items.length < 1) {
					dataStack.items = [];
					dataStack.dataCurrentIndex = 0;

				} else {
					dataStack.dataCurrentIndex = items.length - 2;
					dataStack.items[items.length - 1] = null;
					dataStack.items.splice(items.length - 1, 1);
				}

				// alert(currentObject.cityName);
				Piece.Session.saveObject(stackKey, dataStack);
				return currentObject;

			} else {
				return null;
			}

		},
		deleteObject: function(key) {
			var stackKey = key + '-stack';
			Piece.Session.deleteObject(stackKey);
			Piece.Session.deleteObject(key);
		},
		pullStack: function(keys) {
			var lastUrl = window.localStorage['lastUrl'];
			if (me.stepForward < 0) {
				if (keys.length > 0) {
					for (var i = 0; i < keys.length; i++) {
						if (lastUrl.indexOf(keys[i]) >= 0) {
							var keyArray = keys[i].split('/');
							var keyString = keyArray[0] + '-' + keyArray[1] + '.html';
							this.pullObject(keyString);
						}
					}
				} else {
					if (lastUrl.indexOf(keys) >= 0) {
						this.pullObject(keys);
					}
				}
			}
		},
		go: function() {
			var dataStack = Piece.Session.loadObject('com.csair.airport-meteorological.html-stack');
			// currentObject = dataStack.items.length;
		}
	}

	var Store = {
		saveObject: function(key, object) {
			window.localStorage[key] = JSON.stringify(object);
		},
		loadObject: function(key) {
			var objectString = window.localStorage[key];
			return objectString == null ? null : JSON.parse(objectString);
		},
		deleteObject: function(key) {
			window.localStorage[key] = null;
		},
		clear: function() {
			window.localStorage.clear();
		}
	}

	var Cache = {
		put: function(key, object) {
			if (window.cache == null) {
				window.cache = {}
			};

			window.cache[key] = object;
		},
		get: function(key) {
			if (window.cache == null) {
				return null;
			};

			return window.cache[key];
		}
	}

	Storage.Session = Session;
	Storage.Store = Store;
	Storage.Cache = Cache;



	return Storage;
});